layui.use(['admin', 'table', 'form', 'tool', 'laydate'], function () {
    let $ = layui.$
        , admin = layui.admin
        , setter = layui.setter
        , view = layui.view
        , table = layui.table
        , form = layui.form
        , tool = layui.tool
        , laydate = layui.laydate;

    form.render(null, 'adminLogPage');

    //日期范围
    laydate.render({
        elem: '#adminLogDate'
        ,type: 'datetime'
        ,range: true
    });

    // 管理员管理
    table.render({
        elem: '#adminLogTable'
        , url: setter.request.domainName + '/admin/auth.adminLog/index?server=true'
        , toolbar: true
        , defaultToolbar: setter.defaultToolbar
        , page: true
        , cols: [[
            {field: 'id', title: 'ID', sort: true}
            , {field: 'username', title: '用户名'}
            , {field: 'url', title: 'URL'}
            , {field: 'title', title: '标题'}
            , {field: 'ip', title: 'IP'}
            , {field: 'create_time', title: '创建时间', sort: true,
                templet: function(d) {
                    return tool.timestampToTime(d.create_time);
                }
            }
            , {title: '操作', align: 'center', fixed: 'right', toolbar: '#adminLogTableTool'}
        ]]
        , parseData: function(res){
            let code = res.code === 1 ? 0 : res.code;
            return {
                "code": code, // 解析接口状态
                "msg": res.msg, // 解析提示文本
                "count": res.data.total, // 解析数据长度
                "data": res.data.list // 解析数据列表
            };
        }
        , text: {none: '暂无数据！'}
    });

    // 工具条
    table.on('tool(adminLogTable)', function (obj) {
        let data = obj.data;
        if (obj.event === 'info') {
            admin.popup({
                title: '查看详情'
                , area: ['500px', '750px']
                , id: 'adminLog-popup-info'
                , success: function () {
                    let layId = this.id;
                    let field = {id: data.id};
                    // 获取角色信息
                    admin.req({
                        url: setter.request.domainName + '/admin/auth.adminLog/info'
                        , data: field
                        , type: 'get'
                        , loading: false // 是否开启加载框
                        , done: function (res) {
                            let initData = res.data.row;
                            initData.create_time = tool.timestampToTime(initData.create_time);
                            view(layId).render('auth/adminLog/info', initData).done(function () {
                                form.render(null, 'layPopup');
                            });
                        }
                    })
                }
            });
        }
    });

    //开启搜索
    form.on('submit(adminLogSearch)', function(data){
        let search = [];
        let searchArr = [];
        let nodeList = $(".adminLogSearchForm").find(".searchField");
        nodeList.each(function (i, e) {
            let searchData = {};
            let objNode = $(e);
            let val = objNode.val();
            if(!val && val !== 0  && val !== '0'){
                return true;
            }
            searchData['field'] = objNode.attr("name");
            searchData['val'] = val;
            searchData['operator'] = objNode.attr("operator");
            searchData['render'] = objNode.attr("render");
            search.push(searchData);
            searchArr['search'] = search;
        })
        // 执行重载
        table.reload('adminLogTable', {
            where: searchArr
        });
    });
})
